package yanyan.com.tochar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;


import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import yanyan.com.tochar.beans.WebAppInfo;
import yanyan.com.tochar.test.CardFragmentPagerAdapter;
import yanyan.com.tochar.test.CardItem;
import yanyan.com.tochar.test.CardPagerAdapter;
import yanyan.com.tochar.test.LoopTransformer;
import yanyan.com.tochar.test.ShadowTransformer;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.ImageDialog;
import yanyan.com.tochar.utils.PictureSelectUtil;
import yanyan.com.tochar.utils.ToastUtil;
import zhou.colorpalette.ColorSelectDialog;

public class QqCardActivity  extends FragmentActivity {
    private ViewPager mViewPager;

    private CardPagerAdapter mCardAdapter;
    private ShadowTransformer mCardShadowTransformer;
    private CardFragmentPagerAdapter mFragmentCardAdapter;
    private ShadowTransformer mFragmentCardShadowTransformer;
    private EditText qqEd;
    private ImageView resImg;
    private int fontColor= Color.BLACK;
    private ColorSelectDialog colorSelectDialog;
    private Button colorBtn;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qq_card);
        mViewPager=findViewById(R.id.qq_card_viewpager);
        qqEd=findViewById(R.id.qq_card_acc);
        resImg=findViewById(R.id.qq_card_resImg);
        colorBtn=findViewById(R.id.qq_card_color_btn);

        mCardAdapter = new CardPagerAdapter();
        mCardAdapter.addCardItem(new CardItem("满级会员", BitmapFactory.decodeResource(getResources(), R.drawable.qq_card_1_0)));
        mCardAdapter.addCardItem(new CardItem("葫芦娃",BitmapFactory.decodeResource(getResources(), R.drawable.qq_card_2_0)));
        mCardAdapter.addCardItem(new CardItem("豪车",BitmapFactory.decodeResource(getResources(), R.drawable.qq_card_3_0)));
        mCardAdapter.addCardItem(new CardItem("穿越火线",BitmapFactory.decodeResource(getResources(), R.drawable.qq_card_4_0)));
        mCardAdapter.addCardItem(new CardItem(" 王者荣耀",BitmapFactory.decodeResource(getResources(), R.drawable.qq_card_5_0)));
        mCardAdapter.addCardItem(new CardItem("QQ飞车",BitmapFactory.decodeResource(getResources(), R.drawable.qq_card_6_0)));
        mFragmentCardAdapter = new CardFragmentPagerAdapter(getSupportFragmentManager(),
                dpToPixels(2, this));

        mCardShadowTransformer = new ShadowTransformer(mViewPager, mCardAdapter);
        mFragmentCardShadowTransformer = new ShadowTransformer(mViewPager, mFragmentCardAdapter);

        mViewPager.setAdapter(mCardAdapter);
        mViewPager.setPageTransformer(false, mCardShadowTransformer);
        mViewPager.setOffscreenPageLimit(3);


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                ToastUtil.showLongToast(QqCardActivity.this,"这是"+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //统计各功能
        CoreUtil.statisticalFuns(this,"qq_card");

    }

    public static float dpToPixels(int dp, Context context) {
        return dp * (context.getResources().getDisplayMetrics().density);
    }

    private Bitmap resBitmap;
    public void createQqCard(View view){
      int index=  mViewPager.getCurrentItem();
      Resources res = QqCardActivity.this.getResources();
        Bitmap bmp=null;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled=false;
        options.inPreferredConfig=Bitmap.Config.ARGB_8888;
      switch (index){
          case 0:
              bmp= BitmapFactory.decodeResource(res, R.drawable.qq_card_1_1,options).copy(Bitmap.Config.ARGB_8888, true);
              resBitmap = cvsTextToImg(122, 85, 30,50, qqEd.getText().toString(), bmp);
              break;
          case 1:
              bmp= BitmapFactory.decodeResource(res, R.drawable.qq_card_2_1,options).copy(Bitmap.Config.ARGB_8888, true);
              resBitmap = cvsTextToImg(100, 60, 17,30,  qqEd.getText().toString(), bmp);
              break;
          case 2:
              bmp= BitmapFactory.decodeResource(res, R.drawable.qq_card_3_1,options).copy(Bitmap.Config.ARGB_8888, true);
              resBitmap = cvsTextToImg(105, 52, 17,30,   qqEd.getText().toString(), bmp);
              break;
          case 3:
              bmp= BitmapFactory.decodeResource(res, R.drawable.qq_card_4_1,options).copy(Bitmap.Config.ARGB_8888, true);
              resBitmap = cvsTextToImg(100, 55, 17,30,  qqEd.getText().toString(), bmp);
              break;
          case 4:
              bmp= BitmapFactory.decodeResource(res, R.drawable.qq_card_5_1,options).copy(Bitmap.Config.ARGB_8888, true);
              resBitmap = cvsTextToImg(120, 60, 20,40,  qqEd.getText().toString(), bmp);
              break;
          case 5:
              bmp= BitmapFactory.decodeResource(res, R.drawable.qq_card_6_1,options).copy(Bitmap.Config.ARGB_8888, true);
              resBitmap = cvsTextToImg(105, 60, 18,30,  qqEd.getText().toString(), bmp);
              break;

      }

        ImageDialog dialog=new ImageDialog(this, "生成成功", "重新生成", "保存", resBitmap, null, new ImageDialog.RightOnlistener() {
            @Override
            public void rightOnclick() {
                String path = PictureSelectUtil.savePhotoToSD(resBitmap, QqCardActivity.this);
                Bitmap b=null;
                switch (index){
                    case 0:
                        b= BitmapFactory.decodeResource(res, R.drawable.qq_card_1_2,options).copy(Bitmap.Config.ARGB_8888, true);
                        break;
                    case 1:
                        b= BitmapFactory.decodeResource(res, R.drawable.qq_card_2_2,options).copy(Bitmap.Config.ARGB_8888, true);
                        break;
                    case 2:
                        b= BitmapFactory.decodeResource(res, R.drawable.qq_card_3_2,options).copy(Bitmap.Config.ARGB_8888, true);
                        break;
                    case 3:
                        b= BitmapFactory.decodeResource(res, R.drawable.qq_card_4_2,options).copy(Bitmap.Config.ARGB_8888, true);
                        break;
                    case 4:
                        b= BitmapFactory.decodeResource(res, R.drawable.qq_card_5_2,options).copy(Bitmap.Config.ARGB_8888, true);
                        break;
                    case 5:
                        b= BitmapFactory.decodeResource(res, R.drawable.qq_card_6_2).copy(Bitmap.Config.ARGB_8888, true);
                        break;
                }
                PictureSelectUtil.savePhotoToSD(b, QqCardActivity.this);
                ToastUtil.showLongToast(QqCardActivity.this,"图片保存成功，路径"+path);
            }
        });
      dialog.show();
    }


    private Bitmap cvsTextToImg(int x,int y,int prop,int fontSize,String text,Bitmap bitmap){
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Canvas canvas=new Canvas(bitmap);
        //画颜色的笔
        Paint colorPaint=new Paint();
        colorPaint.setStyle(Paint.Style.FILL);
        colorPaint.setTextAlign(Paint.Align.CENTER);
        colorPaint.setTextSize(fontSize);
        colorPaint.setColor(fontColor);
        int len=text.length();
        for(int i=0;i<len;i++){
            int xAd=i*prop;
            String str= String.valueOf(text.charAt(i));
            canvas.drawText(str,x+xAd,y,colorPaint);
        }
        return bitmap;
    }


    /**
     * 颜色选择
     */
    public void cardSelectColor(View view){
        if (colorSelectDialog == null) {
            colorSelectDialog = new ColorSelectDialog(this);
        }
        colorSelectDialog.setOnColorSelectListener(new ColorSelectDialog.OnColorSelectListener() {
            @Override
            public void onSelectFinish(int color) {
                fontColor=color;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        colorBtn.setBackgroundColor(color);
                    }
                });
            }
        });
        colorSelectDialog.show();
    }


    public void getCourse(View view){
        WebAppInfo webAppInfo=new WebAppInfo();
        webAppInfo.setId(111);
        webAppInfo.setTitle("装X名片制作教程");
        webAppInfo.setUrl("https://shimo.im/docs/tDqkDxXTDqdqrdHt/");
            Intent in=new Intent();
            in.setClass(this,FunsWebAppActivity.class);
            in.putExtra("webapp", webAppInfo);
            startActivity(in);
    }
}
